package com.sita.tboard.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.event.LogoutEvent;
import com.sita.bike.model.PersonalInfo;
import com.sita.bike.model.PersonalInfoListener;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.ui.activity.AboutActivity;
import com.sita.bike.utils.AccountUtils;
import com.sita.tboard.global.LocalConstants;
import com.sita.tboard.hitchhike.HitchhikeActivity;
import com.sita.tboard.ui.activity.MyRtResourcesActivity;
import com.sita.tboard.ui.activity.RankingActivity;
import com.sita.tboard.ui.my.MyProfileActivity;
import com.sita.tboard.util.DateUtils;
import com.sita.tboard.util.NumberUtils;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

//import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MineFragment extends Fragment {

    @Bind(R.id.image_background)
    ImageView mHeaderBackground;
    @Bind(R.id.nickname)
    TextView mNicknameView;
    @Bind(R.id.mileage)
    TextView mMileage;
    @Bind(R.id.drive)
    TextView mDrive;
    @Bind(R.id.rank)
    TextView mRank;
    @Bind(R.id.trend_num)
    TextView mTrendNum;

    @Bind(R.id.trend_line)
    RelativeLayout rl_trendLine;
    @Bind(R.id.maintain_line)
    RelativeLayout rl_maintainLine;
    @Bind(R.id.profile_line)
    RelativeLayout rl_profileLine;
    @Bind(R.id.blacklist_line)
    RelativeLayout rl_blacklistLine;
    @Bind(R.id.about_line)
    RelativeLayout rl_aboutLine;

    @Bind(R.id.hitchhike_line)
    RelativeLayout rl_hitchhikeLine;

    public MineFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle bundle) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, view);

        rl_trendLine.setClickable(false);
        rl_maintainLine.setClickable(true);
        rl_profileLine.setClickable(true);
        rl_blacklistLine.setClickable(true);
        rl_aboutLine.setClickable(true);
        mRank.setClickable(false);

        if (LocalConstants.isHitchhikeEnabled()) {
            rl_hitchhikeLine.setVisibility(View.VISIBLE);
        } else {
            rl_hitchhikeLine.setVisibility(View.GONE);
        }

        initViewWithAccount();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            refreshData();
        }
    }

    private void setClickable(View view) {
        if (!view.isClickable()) {
            view.setClickable(true);
        }
    }

    private void initViewWithAccount() {
        Picasso.with(GlobalContext.getGlobalContext()).load(AccountUtils.getAvatar())
                .fit().centerInside().into(mHeaderBackground);
        mNicknameView.setText(AccountUtils.getNickName());
    }

    private void refreshData() {
        AccountUtils.getRankingInfoAsync(mPersonalInfoListener);
        AccountUtils.getTrackInfoAsync(mPersonalInfoListener); // no need to refresh every time
        AccountUtils.getTrendCountAsync(mPersonalInfoListener); // no need to refresh every time
    }

    private PersonalInfoListener mPersonalInfoListener = new PersonalInfoListener() {
        @Override
        public void Failed() {

        }

        @Override
        public void Success(PersonalInfo info) {
            if (info.isRank()) {
                mRank.setText(String.valueOf(info.getRank()));
                setClickable(mRank);
            }

            if (info.isTotal()) {
                String unitKm = "km";
                mMileage.setText(NumberUtils.formatNumber(info.getTotal() / 1000) + " " + unitKm);
            }

            if (info.isTime()) {
                CharSequence duration = DateUtils.formatDuration(info.getTime());
                mDrive.setText(duration);
            }

            if (info.isTrendCount()) {
                mTrendNum.setText(String.valueOf(info.getTrendCount()));
                rl_trendLine.setClickable(true);
            }
        }
    };

    @OnClick(R.id.trend_line)
    public void clickTrendLine() {
        Intent intent = new Intent(getActivity(), MyRtResourcesActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.maintain_line)
    public void clickMaintainLine() {
        Intent intent = new Intent(getActivity(), MyProfileActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.profile_line)
    public void clickProfileLine() {
        Intent intent = new Intent(getActivity(), MyProfileActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.blacklist_line)
    public void clickBlacklistLine() {
        Intent intent = new Intent(getActivity(), MyProfileActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.quit_line)
    public void clickQuitLine() {
        EventBus.getDefault().post(new LogoutEvent());
    }

    @OnClick(R.id.about_line)
    public void clickAboutLine() {
        Intent intent = new Intent(getActivity(), AboutActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.rank)
    public void clickRank() {
        Intent intent = new Intent(getActivity(), RankingActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.hitchhike_line)
    void onClickCallcar(){
        startActivity(HitchhikeActivity.newIntent(null));
    }
}
